package com.esm.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.esm.model.User;
import com.esm.service.LoginService;
import com.esm.serviceimpl.LoginServiceImpl;
import com.esm.utils.PrintWriterUtil;

import net.sf.json.JSONObject;





/**
 * 
 * @author 王文成
 *
 */
public class LoginController extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	LoginService lService = new LoginServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
	{
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
	{
		HttpSession session = req.getSession();
		//获取登陆请求参数
		String uname = req.getParameter("uname");
		String upsw = req.getParameter("upsw");
		//根据请求参数初始化用户对象
		User tempUser = new User(uname,upsw);
		//调用用户登陆服务
		User currentUser = lService.userLogin(tempUser);
		//将用户对象存入session中




		session.setAttribute("currentUser", currentUser);
		PrintWriter pw = resp.getWriter();
		JSONObject jo = new JSONObject();
		boolean flag = false;
		if(currentUser.getUid() !=0)
		{
			flag = true;
		}
		jo.put("flag", flag);
		PrintWriterUtil.flushJoAndClosePrintWriter(pw, jo);
	}
}
